package dao

import (
	"github.com/benji/software/model"
	log "github.com/sirupsen/logrus"
)

func AddHistory(history *model.History) error {
	if err := Db.Table("history").Save(history).Error; err != nil {
		log.Errorf("AddHistory failed ,err: %v", err)
		return err
	}
	return nil
}
func GetHistoryByStudentID(studentID int) ([]*model.History, error) {
	res := make([]*model.History, 0)
	if err := Db.Table("history").Where("student = ? ", studentID).Find(&res).Error; err != nil {
		log.Errorf("GetHistoryByStudentID failed ,err: %v", err)
		return nil, err
	}
	return res, nil
}
func GetHistoryByID(historyID int) (*model.History, error) {
	res := &model.History{}
	if err := Db.Table("history").Where("id = ?", historyID).Find(res).Error; err != nil {
		log.Errorf("DB fail to getHistoryID")
		return nil, err
	}
	return res, nil
}
func GetAllHistoryByUserID(ID int) ([]*model.History, error) {
	res := make([]*model.History, 0)
	if err := Db.Table("history").Where("student = ?", ID).Find(&res).Error; err != nil {
		log.Errorf("DB fail to getAllHistoryByID: %v", err)
		return nil, err
	}
	return res, nil
}
func UpdateHistory(history *model.History) error {
	if err := Db.Table("history").Where("id = ?", history.ID).Update(history).Error; err != nil {
		log.Errorf("UpdateHistory failed ,err: %v", err)
		return err
	}
	return nil
}
